/**
 * Created by Administrator on 2017/1/16.
 */

var MJPlayer = require('../MJ/player').MJPlayer;
var MJ = require('../MJ/MJ');
var Const = require('./const');
var SubCode = require('../subCode');

exports.XQMJPlayer = MJPlayer.extend({
    _gangTasks: [],

    init: function (uid, msg) {
        this._super(uid, msg);

        this._stat.zmCount = 0;             // 自摸次数
        this._stat.dpCount = 0;             // 点炮次数
        this._stat.dhCount = 0;             // 点胡次数
        this._stat.anGangCount = 0;        // 暗杠次数
        this._stat.mingGangCount = 0;      // 明杠次数
        this._stat.lzCount = 0;             // 庄家次数
        this._stat.roundRules = {};         // 本轮加法玩法

        this._gangTasks = [];
    },

    getPengCount: function () {
        return this._pengCards.length;
    },

    //计算杠分
    settlementGangScore: function () {
        var player = this;
        var room = player.getRoom();        //房间信息
        var gameRule = room.getOption("RULE");      // 房间规则信息
        var dealer = room.getPlayerByIndex(room.getDealerIndex());  //庄的位置

        for (var i = 0, l = player._gangCards.length; i < l; ++i) {
            var gangInfo = player._gangCards[i];
            if (gangInfo.unscored) {
                continue;
            }

            var score = Const.GangScore[gangInfo.gang];
            if (gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                room.enumPlayers(function (eIndex, ePlayer) {
                    if (eIndex != player.getIndex()) {
                        player.addRoundGangScore(score);
                        ePlayer.addRoundGangScore(-score);
                    }
                });
            } else if (gangInfo.gang == MJ.Gang.DIAN || gangInfo.gang == MJ.Gang.BA) {
                var dgPlayer = room.getPlayerByIndex(gangInfo.from);
                dgPlayer.addRoundGangScore(-score);
                player.addRoundGangScore(score);
            }
        }
    },

    settlementPatternScore: function () {
        var player = this;
        var room = this.getRoom();

        // 计算每个人的加分项
        room.enumPlayers(function (eIndex, ePlayer) {
            ExObject.each(ePlayer._stat.roundRules, function (rule, value) {
                ePlayer.addRoundItemScores(rule, Const.ItemScore[rule]);
                ePlayer.setRoundScoreRule(rule);
            });
        });

        if (!this.isRoundHu()) {
            return;
        }

        // 结算牌型及基础分
        var basicScore = this._stat.roundPatternScore + ExObject.sumValue(this._stat.roundItemScores) + this._stat.roundGangScore;

        this._stat.roundTarget.forEach(function (playerIndex) {
            var target = room.getPlayerByIndex(playerIndex);
            if (target) {
                target.addRoundScore(-basicScore);
                target.addRoundScoreDetail(player.getRoundResult(), -basicScore);
                player.addRoundScore(basicScore);
            }
        });
    },

    settlementRules: function () {
        //以下规则只针对赢家有效
        if (!this.isRoundHu()) {
            return;
        }
    },

    settlement: function () {
        this._super();

        this.addRoundScore(this._stat.roundGangScore);

        if (this._stat.roundResult == MJ.Result.ZM) {
            this._stat.zmCount++;
        } else if (this._stat.roundResult == MJ.Result.DH) {
            this._stat.dhCount++;
            if (this._stat.roundTarget.length > 0) {
                var dpPlayer = this.getRoom().getPlayerByIndex(this._stat.roundTarget[0]);
                if (dpPlayer) {
                    dpPlayer._stat.dpCount++;
                }
            }
        }

        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.type) {

            } else {
                if (gangInfo.gang == MJ.Gang.AN || gangInfo.gang == MJ.Gang.ANS) {
                    this._stat.anGangCount++;
                } else {
                    this._stat.mingGangCount++;
                }
            }
        }

        if (this.isDealer()) {
            this._stat.lzCount++;
        }
    },

    /******************************************************************
     * 重载的接口
     ******************************************************************/

    getReconnectInfo: function (isSelf) {
        var info = this._super(isSelf);
        // info.selectPiao = this._selectPiao;
        return info;
    },

    cancelLastGangForQG: function (card) {
        var gangIndex = this._gangCards.length - 1;
        if (gangIndex < 0) {
            return;
        }

        var gangInfo = this._gangCards[gangIndex];
        if (gangInfo.card != card) {
            return;
        }

        this.getRoom().broadcast("onCancelGangForQG", {
            playerIndex: this.getIndex(),
            gangInfo: gangInfo,
        });

        if (gangInfo.gang == MJ.Gang.BA) {
            this._pengCards.push(card);
            this._playCards.push({card: card, task: MJ.Task.HU});
        } else if (gangInfo.gang == MJ.Gang.DIAN) {
            this._handCards.addCard(card, 3);
        } else {
            this._handCards.addCard(card, 3);
            this._playCards.push({card: card, task: MJ.Task.HU});
        }

        this._gangCards.splice(gangIndex, 1);
        return gangInfo;
    },

    getSettlementInfo: function () {
        var info = this._super();
        info.gangCards = this._gangCards;
        info.pengCards = this._pengCards;
        info.chiCards = this._chiCards;
        info.handCards = this._handCards.getCards();
        info.pengCardsFrom = this._pengCardsFrom;
        return info;
    },

    // gangCard: function (gangTask) {
    //     var selectGang = gangTask.selectGang || 0;
    //     if (selectGang > gangTask.gangArray.length) {
    //         selectGang = 0;
    //     }
    //
    //     var selectGangTask = gangTask.gangArray[selectGang];
    //
    //     return selectGangTask;
    // },

    onStartNewRound: function () {
        this._super();
        this._selectPiao = -1;
        this._stat.roundRules = {};
        this._stat.roundPiaoScore = 0;
        this._gangTasks = [];
    },

    gangCard: function (gangTask) {
        var selectGang = gangTask.selectGang || 0;
        if (selectGang > gangTask.gangArray.length) {
            selectGang = 0;
        }

        var selectGangTask = gangTask.gangArray[selectGang];

        if (selectGangTask.type) {
            return this._specGangCard(selectGangTask);
        } else {
            return this._super(selectGangTask);
        }
    },

    _getSpecGang: function (type) {
        var result = null;
        for (var i = 0, l = this._gangCards.length; i < l; ++i) {
            var gangInfo = this._gangCards[i];
            if (gangInfo.type == type) {
                result = gangInfo;
                break;
            }
        }
        return result;
    },

    _specGangCard: function (gangTask) {
        var gangInfo = null;
        var self = this;

        if (gangTask.buCard) {
            gangInfo = this._getSpecGang(gangTask.type);
            if (gangInfo) {
                if (!gangInfo.buCards) {
                    gangInfo.buCards = [];
                }

                var buCard = gangTask.buCard;
                if (this._handCards.getCardNum(buCard) > 0) {
                    this._handCards.delCard(buCard);
                    gangInfo.buCards.push(buCard);
                }

                gangInfo.buCard = gangTask.buCard;
            }
        } else if (gangTask.cards) {
            gangTask.cards.forEach(function (card) {
                this._handCards.delCard(card);
            }.bind(this));

            gangInfo = clone(gangTask);
            delete gangInfo.playerIndex;
            this._gangCards.push(gangInfo);
        }

        return gangInfo;
    },

    /**
     * 检查杠
     * @returns []
     */
    checkGang: function () {
        var room = this.getRoom();
        var gangArray = [];

        var lastCard = this._handCards.getLastNewCard();
        if (this.getIndex() != room.getCurPlay() || room.getCurTask()) {
            lastCard = 0;
        }

        if (lastCard) { //优先杠刚摸上的牌
            if (this._handCards.getCardNum(lastCard) == 4) {
                gangArray.push({card: lastCard, gang: MJ.Gang.AN});
            }

            if (this._pengCards.indexOf(lastCard) != -1) {
                gangArray.push({card: lastCard, gang: MJ.Gang.BA});
            }
        }

        var anGangCards = this._handCards.getAnGangCards();
        for (var i = 0; i < anGangCards.length; ++i) {
            if (anGangCards[i] != lastCard) {
                var gangInfo = {card: anGangCards[i], gang: MJ.Gang.ANS};
                if (this.getRoom().getHideOption("anGangOverdue")) {
                    if (this.getPlayHand() != this.getRoom().getCurGang()) {
                        gangInfo.overdue = true;
                    }
                }
                gangArray.push(gangInfo);
            }
        }

        this._pengCards.forEach(function (card) {
            if (this._handCards.getCardNum(card) > 0) {
                if (card != lastCard) {
                    gangArray.push({card: card, gang: MJ.Gang.BA, overdue: true});
                }
            }
        }.bind(this));

        var from = this.getIndex();
        gangArray.forEach(function (gangInfo) {
            gangInfo.from = from;
        });

        return gangArray;
    },

    /***
     * 检查扎杠（死杠是扎杠的一种）
     * @returns {boolean}
     */
    checkZhaGang: function () {
        var room = this.getRoom();
        var lastCard = room._lastCard;
        if (this.checkGangWithCard(lastCard)) {
            return true;
        }
        return false;
    },

    // 检查胡牌逻辑
    checkHuLogic: function (cards, huCard) {
        var room = this.getRoom();      // 检测扎杠或死杠
        if (this.checkZhaGang() && (room.specialMode["ZG"] == 1 || room.specialMode["SG"] == 1)) {
            return Const.Pattern.ZHAGANG;
        }

        var pattern = this._super(cards, huCard);
        if (pattern == MJ.Pattern.PAIR7) {
            return Const.Pattern.NONE;
        } else if (pattern >= MJ.Pattern.NORMAL) {
            return Const.Pattern.NORMAL;
        }

        return Const.Pattern.NONE;
    },

    // 预设牌型
    getInitHandCardPreCards: function () {
        var preCards = [];

        var distance = this.getRoom().getIndexDistance(this.getIndex(), this.getRoom().getDealerIndex());
        if (distance == 0) {
            // preCards.push(MJ.getValue(MJ.Color.TONG, 4));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 4));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 4));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 5));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 5));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 5));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 5));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 2));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 7));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 7));

            // preCards.push(MJ.getValue(MJ.Color.TONG, 3));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 3));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 1));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 4));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 4));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 2));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 2));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 2));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 4));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 9));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 6));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 6));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 7));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 1));


            // kd duizi
            // preCards.push(MJ.getValue(MJ.Color.WAN, 1));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 1));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 5));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 5));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 7));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 7));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 7));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 8));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 9));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 8));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 1));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 5));
            //kd lianzi
            // preCards.push(MJ.getValue(MJ.Color.WAN, 1));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 1));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 5));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 5));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 6));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 6));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 6));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 4));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 6));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 8));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 9));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 7));
        } else if (distance == 1) {
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 3));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 6));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 6));
            // preCards.push(MJ.getValue(MJ.Color.WAN, 6));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 2));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 2));
            // preCards.push(MJ.getValue(MJ.Color.TIAO, 2));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.TONG, 8));

            // preCards.push(MJ.getValue(MJ.Color.ZI, 1));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 2));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 4));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 5));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 6));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 7));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 1));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 2));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 3));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 4));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 5));
            // preCards.push(MJ.getValue(MJ.Color.ZI, 6));
        }
        return preCards;
    },
});